package com.example.myapplication.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;

import android.util.AttributeSet;
import android.view.View;

/**
 * 水波纹特效
 */
public class HeadWaveView extends AppCompatImageView {
    private boolean mIsRunning;
    AnimationDrawable mAnimationDrawable;

    public HeadWaveView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mAnimationDrawable = (AnimationDrawable) getDrawable();
    }
    public void start() {
        if (!mIsRunning) {
            mIsRunning = true;
            setVisibility(View.VISIBLE);
            mAnimationDrawable.start();
        }
        mHandler.removeMessages(1);
        mHandler.sendEmptyMessageDelayed(1, 1581);
    }

    Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                stop();
            }
        }
    };

    public void stop() {
        mHandler.removeMessages(1);
        mAnimationDrawable.stop();
        setVisibility(View.GONE);
        mIsRunning = false;
    }
}